import time
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
import pandas as pd
# 最初版的爬虫,bug未修复，爬取不到题目难度和标签不存在的题
# 可配置部分

destin_problem_route = "problems_infoB3.xlsx"
prefix = 'P'
left = 1000
right = 9999

# end

# 创建一个空的 DataFrame
data = {
    "Problem Number": [],
    "Problem Name": [],
    "Difficulty": [],
    "Tags": [],
    "Submission Count": [],
    "Accepted Count": [],
    "Acceptance Rate":[]
}

def parse_number(num_str):
    """解析带有 M 和 K 的字符串，转换为浮点数"""
    # print(num_str)
    num_str = num_str.strip()   # 去除前后空格
    last_char = num_str[len(num_str)-1]     # 最后一个字符
    new_str = num_str[:-1]      # 去掉最后一个字符的串
    # print(last_char)
    if last_char=='K'or last_char=='k':
        return float(new_str) * 1000.0
    elif last_char=='M'or last_char=='m':
        return float(new_str) * 1000000.0
    else:
        # print("else执行")
        return float(num_str)

# 启动 Chrome 浏览器
browser = webdriver.Chrome()

# 遍历从 P1000 到 P10000 的题目
for i in range(left, right):
    default_url = f"https://www.luogu.com.cn/problem/{prefix}{i}"
    browser.get(default_url)
    browser.implicitly_wait(3)

    try:
        # 点击按钮，展开动态页面
        button = browser.find_element(By.XPATH, "//*[@id='app']/div[2]/main/div/section[1]/div[2]/div[2]/span")
        browser.execute_script("arguments[0].click();", button)

        # 获取问题标题和难度
        problem_title = browser.find_element(By.XPATH, "//*[@id='app']/div[2]/div[1]/div[2]/h1/span").text
        problem_diff = browser.find_element(By.XPATH, "//*[@id='app']/div[2]/main/div/section[1]/div[1]/div/div[2]/span[2]/a/span").text
        problem_tag = browser.find_element(By.XPATH, "//*[@id='app']/div[2]/main/div/section[1]/div[2]/h3").text

        # 将标题分解为题号和题名
        problem_number, problem_name = problem_title.split(" ", 1)

        # 获取多个标签
        tags = browser.find_elements(By.XPATH, "//*[@id='app']/div[2]/main/div/section[1]/div[2]/div[1]/div/a")
        tag_list = []

        for tag in tags:
            # 提取 href 属性中的 tag 数字
            href = tag.get_attribute("href")
            tag_number = href.split("=")[-1]  # 获取 tag= 后的数字
            tag_list.append(tag_number)

        # 获取提交次数和通过次数
        submission_count_str = browser.find_element(By.XPATH, "//*[@id='app']/div[2]/div[1]/div[2]/div[2]/div[1]/div/div[1]/span[2]").text
        accepted_count_str = browser.find_element(By.XPATH, "//*[@id='app']/div[2]/div[1]/div[2]/div[2]/div[1]/div/div[2]/span[2]").text

        # 将提交和通过次数存为字符串
        submission_count = submission_count_str
        accepted_count = accepted_count_str

        # 计算通过率
        Acceptance_Rate=parse_number(accepted_count)/parse_number(submission_count)
        # 将信息存储到 DataFrame 中
        # data["Problem Number"].append(problem_number)
        # data["Problem Name"].append(problem_name)
        # data["Difficulty"].append(problem_diff)
        # data["Tags"].append(", ".join(tag_list))  # 将标签数字列表转换为逗号分隔的字符串
        # data["Submission Count"].append(submission_count)
        # data["Accepted Count"].append(accepted_count)
        # data["Acceptance Rate"].append(Acceptance_Rate)
        data["Problem Number"].append(problem_number if problem_number else "")
        data["Problem Name"].append(problem_name if problem_name else "")
        data["Difficulty"].append(problem_diff if problem_diff else "")
        data["Tags"].append(", ".join(tag_list) if tag_list else "")
        data["Submission Count"].append(submission_count)
        data["Accepted Count"].append(accepted_count)
        data["Acceptance Rate"].append(Acceptance_Rate)
    except Exception as e:
        print(f"Error fetching problem P{i}: {e}")

    time.sleep(0.5)  # 等待一秒，避免过快请求

# 创建 DataFrame
df = pd.DataFrame(data)

# 将 DataFrame 写入 Excel 文件
df.to_excel(destin_problem_route, index=False)

browser.quit()
